<?php

namespace App\Http\Controllers;

use App\Models\Tag;
use App\Models\TagMap;
use App\Models\Article;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class TagsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth', ['except' => ['show']]);
    }

    public function show(Tag $tag)
    {
        $aids = TagMap::where('tid',$tag->id)->pluck('aid');
        // 读取分类 ID 关联的话题，并按每 20 条分页
        $articles = Article::with('user','category')->whereIn('id',$aids)->paginate(15);
        // 传参变量话题和分类到模板中
        return view('tags.show', compact('articles', 'tag'));
    }

}